package pers.xiqingbo.web;

import pers.xiqingbo.pojo.Book;
import pers.xiqingbo.pojo.Page;
import pers.xiqingbo.service.BookService;
import pers.xiqingbo.service.impl.BookServiceImpl;
import pers.xiqingbo.utils.WebUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;

/**
 * @PackageName: ${PACKAGE_NAME}
 * @ClassName: ${NAME}
 * @Description: 首页图书业务分页表示层
 * @Author: Schieber
 * @Date: 2021/2/23 下午 13:16
 */
public class ClientBookServlet extends BaseServlet {
    private BookService bookService = new BookServiceImpl();

    /**
     * 处理分页功能
     *
     * @param request  接收对象
     * @param response 响应对象
     * @throws ServletException Servlet异常
     * @throws IOException      IO异常
     */
    protected void page(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获取当前页码和分页后显示数量的请求参数
        int pageNo = WebUtils.parseInt(request.getParameter("pageNo"), 1);
        int pageSize = WebUtils.parseInt(request.getParameter("pageSize"), Page.PAGE_SIZE);
        // 调用service进行分页业务
        Page<Book> pageBook = bookService.page(pageNo, pageSize);
        pageBook.setUrl("client/bookServlet?action=page");
        // 分页完成后将分页后的图书数据共享到域
        request.setAttribute("pageBook", pageBook);
        // 请求路径转发至图书列表页面
        request.getRequestDispatcher("/pages/client/index.jsp").forward(request, response);
    }

    /**
     * 处理通过价格区间查询后分页功能
     *
     * @param request  接收对象
     * @param response 响应对象
     * @throws ServletException Servlet异常
     * @throws IOException      IO异常
     */
    protected void pricePage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获取当前页码和分页后显示数量的请求参数
        int pageNo = WebUtils.parseInt(request.getParameter("pageNo"), 1);
        int pageSize = WebUtils.parseInt(request.getParameter("pageSize"), Page.PAGE_SIZE);
        // 获取最低和最高价格区间数值的请求参数
        BigDecimal min = WebUtils.parseBigDecimal(request.getParameter("min"), "0.00");
        BigDecimal max = WebUtils.parseBigDecimal(request.getParameter("max"), String.valueOf(Integer.MAX_VALUE));
        // 调用service进行分页业务
        Page<Book> pageBook = bookService.pricePage(min, max, pageNo, pageSize);
        StringBuilder url = new StringBuilder("client/bookServlet?action=pricePage");
        // 解决首页通过价格查询图书后地址栏内没有min和max参数导致不能正常跳转页面的问题
        if (request.getParameter("min") != null){
            url.append("&min=").append(request.getParameter("min"));
        }
        if (request.getParameter("max") != null){
            url.append("&max=").append(request.getParameter("max"));
        }
        // 设置url参数传给分页去获取
        pageBook.setUrl(url.toString());
        // 分页完成后将分页后的图书数据共享到域
        request.setAttribute("pageBook", pageBook);
        // 请求路径转发至图书列表页面
        request.getRequestDispatcher("/pages/client/index.jsp").forward(request, response);
    }
}
